package cn.iocoder.boot.service;

import cn.iocoder.boot.common.RedisKeyConstants;
import cn.iocoder.boot.entity.ComponentSupplier;
import cn.iocoder.boot.entity.KbPlatformApi;
import cn.iocoder.boot.mapper.ComponentSupplierMapper;
import cn.iocoder.boot.mapper.KbPlatformApiMapper;
import cn.iocoder.boot.utils.MessageUtil;
import cn.iocoder.boot.service.impl.ComponentSupplierServiceImpl;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class CallLimitService {

    private static final int MIN_CALLS_PER_DAY = 0;

    @Resource
    private KbPlatformApiMapper platformApiMapper;
    @Resource
    private KbPlatformApiService platformApiService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ComponentSupplierServiceImpl componentSupplierService;
    @Resource
    private ComponentSupplierMapper componentSupplierMapper;
    @Resource
    private RedisTemplate redisTemplate;

    public boolean canCall(Long id) {
        String redisKey = String.format(RedisKeyConstants.PLATFORM_API_TEMPLATE, id);
        KbPlatformApi platformApi = platformApiService.getPlatformApi(id);
        if (Objects.isNull(platformApi)) {
            return false;
        }
        Long currentCount = platformApi.getRemainingNumber();
        if (currentCount <= MIN_CALLS_PER_DAY) {
            return false;
        }

        //剩余量 == 阈值数量 发送告警
        if (Objects.equals(platformApi.getRemainingNumber(), platformApi.getAlarmValue())) {
            // 发送告警
            MessageUtil.sentMessage(platformApi.getNoticeNumber(), "平台API调用量即将耗尽", "平台API告警");
        }
        platformApi.setRemainingNumber(currentCount - 1);
        platformApiMapper.updateById(platformApi);
        //设置缓存
        stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(platformApi), 2, TimeUnit.DAYS);
        return true;
    }


    public void supplierLimitCall(String code){
        String redisKey = String.format(RedisKeyConstants.SUPPLIER_LIMIT, code);
        //获取次数
        Long limit = componentSupplierService.getLimit(code);

        UpdateWrapper<ComponentSupplier> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("no", code)   // 设置查询条件
                .set("last_count", Long.valueOf(limit - 1));     // 转换成 Long 类型


// 执行更新操作
        componentSupplierMapper.update(null, updateWrapper);
        redisTemplate.opsForValue().set(redisKey, limit-1, 2, TimeUnit.DAYS);

    }


//    public void resetCallCount() {
//        platformApiMapper.resetDailyCallCount();
//    }
}
